﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Brain.Logic.AI
{
    public class DefaultAIManager : IAIManager, IUpdateable, IDrawable
    {
        public void DeserializedAll()
        {
        }
        public static Dictionary<int, AIInfo> AICharacters;

        public static bool MaintainThroughResets = true;

        public List<ArtificialIntelligence> AI;
        private List<IDrawable> DrawAI;

        public bool ShouldUpdate { get; set; }
        public bool ShouldDraw { get; set; }

        public DefaultAIManager()
        {
            if (AICharacters == null)
            {
                AICharacters = new Dictionary<int, AIInfo>();
            }

            AI = new List<ArtificialIntelligence>();
            DrawAI = new List<IDrawable>();

            ShouldUpdate = true;
            ShouldDraw = true;
        }
        public void AddAI(object ai)
        {
            this.AI.Add(ai as ArtificialIntelligence);

            if (ai is IDrawable)
            {
                DrawAI.Add(ai as IDrawable);
            }
        }

        public static void AddAI(int aiID, AIInfo info)
        {
            AICharacters.Add(aiID, info);
        }
        public static AIInfo GetAI(int aiID)
        {
            return AICharacters[aiID];
        }

        public void Update()
        {
            for (int i = 0; i < AI.Count; i++)
            {
                ArtificialIntelligence ai = AI[i];
                if (ai.ShouldUpdate)
                {
                    ai.Update();
                    for (int x = 0; x < AI.Count; x++)
                    {
                        if (x != i)
                        {
                            ArtificialIntelligence b = AI[x];
                            ai.Look(b);
                        }
                    }
                }
            }
        }
        public void Draw()
        {
            for (int i = 0; i < DrawAI.Count; i++)
            {
                IDrawable ai = DrawAI[i];
                if (ai.ShouldDraw)
                {
                    ai.Draw();
                }
            }
        }

        public void Reset()
        {
            if (!MaintainThroughResets)
            {
                AICharacters.Clear();
                AICharacters = new Dictionary<int, AIInfo>();
            }
        }

        public SerializationData Data { get; set; }
        public void SerializeData(DefaultSerializationManager manager)
        {
        }
        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }
    }
}
